<template>
  <div class="page-container">
    <div class="content-box">
      <h2>退款管理</h2>
      
      <!-- 统计卡片 -->
      <el-row :gutter="20" class="mb-20">
        <el-col :span="6">
          <div class="stat-card">
            <div class="stat-icon" style="background: #E6A23C;">
              <i class="el-icon-warning-outline"></i>
            </div>
            <div class="stat-content">
              <div class="stat-title">待审核</div>
              <div class="stat-value">{{ statistics.pendingRefunds || 0 }}</div>
            </div>
          </div>
        </el-col>
        
        <el-col :span="6">
          <div class="stat-card">
            <div class="stat-icon" style="background: #409EFF;">
              <i class="el-icon-s-goods"></i>
            </div>
            <div class="stat-content">
              <div class="stat-title">处理中</div>
              <div class="stat-value">{{ statistics.processingRefunds || 0 }}</div>
            </div>
          </div>
        </el-col>
        
        <el-col :span="6">
          <div class="stat-card">
            <div class="stat-icon" style="background: #67C23A;">
              <i class="el-icon-circle-check"></i>
            </div>
            <div class="stat-content">
              <div class="stat-title">已完成</div>
              <div class="stat-value">{{ statistics.completedRefunds || 0 }}</div>
            </div>
          </div>
        </el-col>
        
        <el-col :span="6">
          <div class="stat-card">
            <div class="stat-icon" style="background: #F56C6C;">
              <i class="el-icon-money"></i>
            </div>
            <div class="stat-content">
              <div class="stat-title">退款总额</div>
              <div class="stat-value">¥{{ formatAmount(statistics.totalRefundAmount) }}</div>
            </div>
          </div>
        </el-col>
      </el-row>
      
      <!-- 搜索筛选 -->
      <el-form :model="queryForm" :inline="true" class="search-form">
        <el-form-item label="退款单号">
          <el-input v-model="queryForm.refundNo" placeholder="请输入退款单号" clearable style="width: 180px"></el-input>
        </el-form-item>
        
        <el-form-item label="订单号">
          <el-input v-model="queryForm.orderNo" placeholder="请输入订单号" clearable style="width: 180px"></el-input>
        </el-form-item>
        
        <el-form-item label="退款状态">
          <el-select v-model="queryForm.refundStatus" placeholder="请选择状态" clearable style="width: 120px">
            <el-option label="申请中" :value="0"></el-option>
            <el-option label="审核中" :value="1"></el-option>
            <el-option label="审核通过" :value="2"></el-option>
            <el-option label="审核拒绝" :value="3"></el-option>
            <el-option label="退款中" :value="4"></el-option>
            <el-option label="退款成功" :value="5"></el-option>
            <el-option label="退款失败" :value="6"></el-option>
          </el-select>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="handleSearch" icon="el-icon-search">搜索</el-button>
          <el-button @click="handleReset" icon="el-icon-refresh">重置</el-button>
        </el-form-item>
      </el-form>
      
      <!-- 退款列表 -->
      <el-table 
        :data="refundList" 
        v-loading="loading"
        stripe
        border>
        
        <el-table-column prop="refundNo" label="退款单号" width="180">
          <template slot-scope="scope">
            <el-link type="primary" @click="viewRefundDetail(scope.row)">{{ scope.row.refundNo }}</el-link>
          </template>
        </el-table-column>
        
        <el-table-column prop="orderNo" label="订单号" width="180"></el-table-column>
        <el-table-column prop="studentName" label="学生" width="100"></el-table-column>
        <el-table-column prop="teacherName" label="教师" width="100"></el-table-column>
        
        <el-table-column label="退款金额" width="120">
          <template slot-scope="scope">
            <div style="color: #F56C6C; font-weight: bold;">¥{{ formatAmount(scope.row.applyAmount) }}</div>
          </template>
        </el-table-column>
        
        <el-table-column label="退款状态" width="100">
          <template slot-scope="scope">
            <el-tag :type="getRefundStatusType(scope.row.refundStatus)">
              {{ getRefundStatusText(scope.row.refundStatus) }}
            </el-tag>
          </template>
        </el-table-column>
        
        <el-table-column prop="refundReason" label="退款原因" width="150" show-overflow-tooltip></el-table-column>
        <el-table-column prop="auditUserName" label="审核人" width="100"></el-table-column>
        
        <el-table-column prop="applyTime" label="申请时间" width="150">
          <template slot-scope="scope">
            {{ formatDateTime(scope.row.applyTime) }}
          </template>
        </el-table-column>
        
        <el-table-column label="操作" width="200" fixed="right">
          <template slot-scope="scope">
            <el-button 
              v-if="scope.row.refundStatus <= 1" 
              type="primary" 
              size="mini" 
              @click="auditRefund(scope.row)">
              审核
            </el-button>
            
            <el-button 
              v-if="scope.row.refundStatus === 2" 
              type="success" 
              size="mini" 
              @click="processRefund(scope.row)">
              处理
            </el-button>
            
            <el-button 
              v-if="scope.row.refundStatus === 4" 
              type="warning" 
              size="mini" 
              @click="completeRefund(scope.row)">
              完成
            </el-button>
            
            <el-button type="text" size="mini" @click="viewRefundDetail(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页 -->
      <div class="pagination-wrapper">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryForm.pageNum"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="queryForm.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>
    
    <!-- 退款详情弹窗 -->
    <el-dialog title="退款详情" :visible.sync="detailDialogVisible" width="800px">
      <div v-if="refundDetail">
        <p>退款单号：{{ refundDetail.refundNo }}</p>
        <p>订单号：{{ refundDetail.orderNo }}</p>
        <p>学生：{{ refundDetail.studentName }}</p>
        <p>申请金额：¥{{ formatAmount(refundDetail.applyAmount) }}</p>
        <p>退款原因：{{ refundDetail.refundReason }}</p>
        <p>状态：{{ getRefundStatusText(refundDetail.refundStatus) }}</p>
        <p>申请时间：{{ formatDateTime(refundDetail.applyTime) }}</p>
      </div>
    </el-dialog>
    
    <!-- 审核弹窗 -->
    <el-dialog title="退款审核" :visible.sync="auditDialogVisible" width="500px">
      <el-form :model="auditForm" label-width="100px">
        <el-form-item label="审核结果">
          <el-radio-group v-model="auditForm.refundStatus">
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
        
        <el-form-item label="审核意见">
          <el-input 
            v-model="auditForm.auditRemark" 
            type="textarea" 
            :rows="4"
            placeholder="请输入审核意见">
          </el-input>
        </el-form-item>
      </el-form>
      
      <div slot="footer">
        <el-button @click="auditDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitAudit" :loading="auditLoading">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { 
  getRefundList, 
  getRefundDetail, 
  auditRefund,
  processRefund,
  completeRefund,
  getRefundStatistics 
} from '@/api/finance'

export default {
  name: 'RefundManagement',
  data() {
    return {
      loading: false,
      refundList: [],
      total: 0,
      statistics: {},
      
      queryForm: {
        refundNo: '',
        orderNo: '',
        refundStatus: null,
        pageNum: 1,
        pageSize: 20
      },
      
      detailDialogVisible: false,
      refundDetail: null,
      
      auditDialogVisible: false,
      auditLoading: false,
      auditForm: {
        id: null,
        refundStatus: 2,
        auditRemark: ''
      }
    }
  },
  
  created() {
    this.loadRefundList()
    this.loadStatistics()
  },
  
  methods: {
    async loadRefundList() {
      this.loading = true
      try {
        const response = await getRefundList(this.queryForm)
        if (response.code === 200) {
          this.refundList = response.data.list
          this.total = response.data.total
        }
      } catch (error) {
        this.$message.error('加载退款列表失败')
      } finally {
        this.loading = false
      }
    },
    
    async loadStatistics() {
      try {
        const response = await getRefundStatistics()
        if (response.code === 200) {
          this.statistics = response.data
        }
      } catch (error) {
        console.error('加载统计数据失败', error)
      }
    },
    
    handleSearch() {
      this.queryForm.pageNum = 1
      this.loadRefundList()
    },
    
    handleReset() {
      this.queryForm = {
        refundNo: '',
        orderNo: '',
        refundStatus: null,
        pageNum: 1,
        pageSize: 20
      }
      this.loadRefundList()
    },
    
    handleSizeChange(val) {
      this.queryForm.pageSize = val
      this.queryForm.pageNum = 1
      this.loadRefundList()
    },
    
    handleCurrentChange(val) {
      this.queryForm.pageNum = val
      this.loadRefundList()
    },
    
    async viewRefundDetail(row) {
      try {
        const response = await getRefundDetail(row.id)
        if (response.code === 200) {
          this.refundDetail = response.data
          this.detailDialogVisible = true
        }
      } catch (error) {
        this.$message.error('获取退款详情失败')
      }
    },
    
    auditRefund(row) {
      this.auditForm = {
        id: row.id,
        refundStatus: 2,
        auditRemark: ''
      }
      this.auditDialogVisible = true
    },
    
    async submitAudit() {
      this.auditLoading = true
      try {
        const response = await auditRefund(
          this.auditForm.id,
          this.auditForm.refundStatus,
          this.auditForm.auditRemark
        )
        
        if (response.code === 200) {
          this.$message.success('审核成功')
          this.auditDialogVisible = false
          this.loadRefundList()
        }
      } catch (error) {
        this.$message.error('审核失败')
      } finally {
        this.auditLoading = false
      }
    },
    
    async processRefund(row) {
      try {
        const response = await processRefund(row.id, row.applyAmount, 0)
        if (response.code === 200) {
          this.$message.success('处理成功')
          this.loadRefundList()
        }
      } catch (error) {
        this.$message.error('处理失败')
      }
    },
    
    async completeRefund(row) {
      try {
        const response = await completeRefund(row.id)
        if (response.code === 200) {
          this.$message.success('完成成功')
          this.loadRefundList()
        }
      } catch (error) {
        this.$message.error('完成失败')
      }
    },
    
    formatAmount(amount) {
      return amount ? Number(amount).toFixed(2) : '0.00'
    },
    
    formatDateTime(dateTime) {
      return dateTime ? new Date(dateTime).toLocaleString() : '-'
    },
    
    getRefundStatusType(status) {
      const types = { 
        0: 'warning', 1: 'warning', 2: 'success', 3: 'danger', 
        4: 'warning', 5: 'success', 6: 'danger' 
      }
      return types[status] || ''
    },
    
    getRefundStatusText(status) {
      const texts = { 
        0: '申请中', 1: '审核中', 2: '审核通过', 3: '审核拒绝',
        4: '退款中', 5: '退款成功', 6: '退款失败'
      }
      return texts[status] || '未知状态'
    }
  }
}
</script>

<style lang="scss" scoped>
.stat-card {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  
  .stat-icon {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 16px;
    
    i {
      font-size: 24px;
      color: white;
    }
  }
  
  .stat-content {
    flex: 1;
    
    .stat-title {
      font-size: 14px;
      color: #909399;
      margin-bottom: 8px;
    }
    
    .stat-value {
      font-size: 24px;
      font-weight: bold;
      color: #303133;
    }
  }
}
</style>